package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientComplaint;
import com.woniu.cms.client.core.entity.ClientContact;
import com.woniu.cms.client.core.entity.ClientInfo;
import com.woniu.cms.client.core.service.IClientComplaintService;
import com.woniu.cms.client.core.service.IClientContactService;
import com.woniu.cms.client.core.service.IClientInfoService;
import com.woniu.cms.client.core.vo.ClientComplaintVo;
import com.woniu.cms.client.core.vo.ClientContactVo;
import com.woniu.cms.client.core.vo.ClientInfoVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 本表是客户投诉处理表 前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/client-complaint")
public class ClientComplaintController {

    @Autowired
    private IClientComplaintService iClientComplaintService;


    @Autowired
    private IClientInfoService iClientInfoService;

    @Transactional
    @ApiOperation("保存客户投诉信息")
    @PostMapping
    public Result saveClientComplaint(@RequestBody ClientComplaint clientComplaint){

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientComplaint.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientComplaint.setInfoId(clientInfo.getId());

        boolean save = iClientComplaintService.save(clientComplaint);
        return save ? Result.ok() :Result.error();
    }


    @ApiOperation("根据id删除客户投诉信息")
    @DeleteMapping("/{id}")
    public Result deleteClientComplaint(@PathVariable Integer id){

        boolean b = iClientComplaintService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除客户投诉信息")
    @DeleteMapping
    public Result deleteClientComplaints(@RequestBody List<Integer> ids){
        //批量删除ClientComplaint
        boolean b = iClientComplaintService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    /*这个地方可以再优化一下，然后可以加个非空的注解，确保前端传进来的的值是有值的*/

    @ApiOperation("修改投诉信息")
    @PutMapping
    public Result updateClientComplaint(@RequestBody ClientComplaint clientComplaint){

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientComplaint.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientComplaint.setInfoId(clientInfo.getId());

        boolean save = iClientComplaintService.updateById(clientComplaint);
        return save ? Result.ok() :Result.error();
    }



    @ApiOperation(value = "通过客户列表Id查找客户投诉信息")
    @GetMapping("{id}")
    public  Result findById(@PathVariable Integer id) {

        QueryWrapper<ClientComplaint> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("info_id",id);

        ClientComplaint clientComplaint = iClientComplaintService.getOne(queryWrapper);

        return Result.common(clientComplaint);
    }


    @GetMapping("all")
    @ApiOperation(value = "客户联系人信息的分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientComplaint(Integer currentPage, Integer pageSize){

        IPage<ClientComplaint> ClientComplaintIPage = new Page(currentPage,pageSize);

        IPage<ClientComplaint> page = iClientComplaintService.page(ClientComplaintIPage);

        System.out.println("page = " + page);

        List<ClientComplaint> records = page.getRecords();

        System.out.println("records = " + records);


        List<ClientComplaintVo> clientComplaintVos = copyListProperties(records);

       /* List<ClientInfoVo>  clientInfoVos = records.stream().map(e->{

            ClientComplaintVo clientComplaintVo = new ClientComplaintVo();
            BeanUtils.copyProperties(e,clientComplaintVo);
            return clientComplaintVo;

        });*/

      /*  List<ClientInfoVo>  clientInfoVos = records.stream().forEach(e->{

            ClientComplaintVo clientComplaintVo = new ClientComplaintVo();
            BeanUtils.copyProperties(e,clientComplaintVo);
            return clientComplaintVo;

        });

        records.forEach(e->{

            ClientComplaintVo clientComplaintVo = new ClientComplaintVo();
            BeanUtils.copyProperties(e,clientComplaintVo);
            return clientComplaintVo;

        }).collect(Collectors);*/

        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("data",clientComplaintVos);
        map.put("total",total);

        return Result.common(map);

    }


    private  List<ClientComplaintVo>  copyListProperties(List<ClientComplaint> records){

        List<ClientComplaintVo> clientComplaintVos  = MyBeanUtils.copyListProperties(records, ClientComplaintVo::new);

        return clientComplaintVos;

    }



}

